<?php
/**
 * 角色管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-08-12
 */
namespace Admin\Controller;

class RoleController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db       =D("Node");
        $this->db_role  =D("Role");
    }
    
    /*
     * 角色列表
     * 
     * @return #
     */
    public function _before_index(){
        $_REQUEST['id'] =array("neq",2);
    }
    
    /*
     * 权限列表
     * 
     * @return #
     */
    public function nodeList(){
        $map['status']  =1;
        $map['level']   =0;
        
        $map1['status'] =1;
        $map1['level']  =1;
        $noeNodeList    =$this->db->where($map)->select();          //顶级节点
        $twoNodeList    =$this->db->where($map1)->select();         //次级节点
        $roleList       =A("Public")->checkRole('',I("request.role_id",'','code')); 
        foreach($roleList as $vo){
            $roles[]=$vo['node_id'];
        }
        $this->assign("oneList",$noeNodeList);
        $this->assign("twoList",$twoNodeList);
        $this->assign("rList",$roles);
        $this->display();
    }
    
    /*
     * 授权
     * 
     * @return #
     */
    public function addAuth(){
        if(!I("request.role_id") && !I("request.nodeId")){
            $this->error("授权失败");
        }
        D("RoleNode")->where(array("role_id"=>I("request.role_id")))->delete();       
        foreach(I("request.nodeId") as $vo){
            $map['role_id'] =I("request.role_id");
            $map['node_id'] =$vo;
            if(!D("RoleNode")->add($map)){
               $this->error("授权失败"); 
            }
        }
        session('nodeList_s',null);
        session('nodeList_t',null);
        $this->success("授权成功");        
    }
    
    /*
     * 删除前置操作
     * @自己所在组不允许删除
     * 
     * @return #
     */
    public function _before_del(){
        if(I("request.id")==2){
            $this->error("不允许删除超级管理员组");
            exit;
        }
        $role   =$this->db_role->find(I("request.id"));
        if($role['name']==getUserRole(session("authId"))){
            $this->error("不允许删除自己所在组");
            exit;
        }
    }
}